<?php
defined('BASEPATH') OR exit('No direct script access allowed');

//用户登陆绑定
class Userbing extends MY_Controller {

	function __construct(){
		parent::__construct();
		
	}	
	
	function CheckLogin(){
		
		return true;
	}

	//start
	/**
	* @name 发送验证码
	* @param mobile ture char 手机号码
	* @return 返回1
	*/
	//end
	function send_code(){
		$mobile = $this->input->post('mobile');		
		
		if(empty($mobile)){
			$this->err_out('手机号码不能为空');
		}
		if(strlen($mobile)!=11){
			$this->err_out('手机号码错误');
		}
		
		$this->load->model('User/CoachModel','Coach');	
		$is_exits = $this->Coach->mobile_is_exits($mobile);
		if(!$is_exits){
			$this->err_out('该号码不存在，请联系管理员添加');
		}
		
		$this->load->model('User/ValidateRecordModel','validate','index');		

		$code = rand(100000,999999);
		$this->validate->save_validate_record($mobile,$code,'login');

		
		$data = array('data'=>1,'message'=>'发送手机号码成功');
		$this->out($data);
	}
	
	//start
	/**
	* @name 会员绑定信息
	* @param mobile ture char 手机号码
	* @param code ture char 验证码
	* @param token ture char 凭证
	* @return 成功返回1
	*/
	//end
	public function mobile_bind(){
		$mobile	= $this->input->post('mobile',null,'');	//手机号码
		$code	= $this->input->post('code',null,'');	//code
		$token	= $this->input->post('token',null,'');	//token
		

		$ver_arr = array(
			'mobile'=>"手机号码不能为空",
			'token'=>"token不能为空",
			'code'=>'验证不能为空'
		);
		
		$result = $this->CheckData($ver_arr,array('mobile'=>$mobile,'token'=>$token,'code'=>$code));
		
		if($result){
			$this->err_out($result);
		}
		
		$this->load->model('User/ValidateRecordModel','validate','index');		
		
		if($this->validate->validate_code($mobile,$code,'login')!==true){
			$this->err_out('验证码错误');
		}
		$this->load->model("User/CoachModel",'Coach');
		$openid =  $this->Coach->token_get_openid($token);
		
		$info = $this->Coach->mobile_getinfo($mobile);

		if(!$info){
			$this->err_out('用户不存在');
		}
		$this->Coach->edit(array('openid'=>$openid),"id='{$info['id']}'");
		
		$data = array();
		$data['data'] 		 = 1;
		$data['message'] = "成功";
		$this->out($data);
	}
	
	//start
	/**
	* @name 获取凭证
	* @param code ture char 微信代码
	* @return 成功返回1
	*/
	//end
	function login(){
		$code	= $this->input->post('code',null,'');	//openid
		
		$openid = $this->get_openid($code);
		if(!$openid){
			$this->err_out("获取凭证失败");
		}
		
		
		$this->load->model("User/CoachModel",'Coach');
		$info = $this->Coach->openid_getinfo($openid);

		
		$token = $this->Coach->openid_get_token($openid);
		
		$arr = array(
			'token'=>$token,
			'is_bing'=>$info?1:0
			
		);
		$this->out(array('data'=>$arr));
	}
	
	
	function get_openid($code){
		if(empty($code)){
			return false;
		}
		
		$this->load->model('Admin/ConfigModel', 'configs','index');
		$AppID = $this->configs->unique_get_info("name","AppID");
		
		
		$AppID = $AppID['info'];
		$AppSecret = $this->configs->unique_get_info("name","AppSecret");
		$AppSecret = $AppSecret['info'];
		
		$url = "https://api.weixin.qq.com/sns/jscode2session?appid=".$AppID."&secret=".$AppSecret."&js_code=".$code."&grant_type=authorization_code";
		
		$union = @file_get_contents($url);
		if(empty($union)){
			return false;
		}
		
		$union = json_decode($union,true);
		
		if(isset($union['errcode'])){
			$this->err_out("获取OPENID失败，具体".$union['errmsg']);
		}

		return $union['openid'];		
		
	}
	
}
